package com.atguigu.guli.service.trade.controller.api;

import com.atguigu.guli.service.base.helper.JwtHelper;
import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.base.result.ResultCodeEnum;
import com.atguigu.guli.service.trade.entity.Order;
import com.atguigu.guli.service.trade.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.xml.transform.Result;
import java.util.List;

/**
 * @author dyqstart
 * @create 2021-07-17-20:47
 * @since 1.0.0
 */
//@CrossOrigin
@RestController
@RequestMapping("/api/trade/order")
@Api(tags = "网站订单管理")
@Slf4j
public class ApiOrderControlller {

    @Autowired
    private OrderService orderService;

    @ApiOperation("新增订单")
    @PostMapping("auth/save/{courseId}")
    public R save(@PathVariable String courseId, HttpServletRequest request){//创建request对象，以保证会员信息的安全性

        String memberId = JwtHelper.getId(request);
        String orderId = orderService.saveOrder(courseId,memberId);
        return R.ok().data("orderId",orderId);
    }

    @ApiOperation("获取订单")
    @GetMapping("auth/get/{orderId}")
    public R get(@PathVariable String orderId, HttpServletRequest request){
        //必须是自己的订单才能查看
        String memberId = JwtHelper.getId(request);
        Order order = orderService.getByOrderId(orderId,memberId);
        return R.ok().data("item",order);
    }

    @ApiOperation( "判断课程是否购买")
    @GetMapping("auth/is-buy/{courseId}")
    public R isBuyByCourseId(@PathVariable String courseId, HttpServletRequest request) {

        String memberId = JwtHelper.getId(request);
        Boolean isBuy = orderService.isBuyByCourseId(courseId,memberId);
        return R.ok().data("isBuy",isBuy);
    }

    @ApiOperation(value = "获取当前用户订单列表")
    @GetMapping("auth/list")
    public R list(HttpServletRequest request) {

        String memberId = JwtHelper.getId(request);
        List<Order> list = orderService.selectByMemberId(memberId);
        return R.ok().data("items",list);
    }

    @ApiOperation(value = "删除订单")
    @DeleteMapping("auth/remove/{orderId}")
    public R remove(@PathVariable String orderId,HttpServletRequest request){

        String memberId = JwtHelper.getId(request);
        boolean result = orderService.removeByOrderId(orderId,memberId);
        if (result){
            return R.ok().message("删除成功");
        }else {
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("查询订单支付状态")
    @GetMapping("query-pay-status/{orderNo}")
    public R queryPayStatus(
            @ApiParam(value = "订单号")
            @PathVariable String orderNo){
        boolean result = orderService.queryPayStatus(orderNo);
        if (result){
            return R.ok().message("支付成功");
        }
        return R.setResult(ResultCodeEnum.PAY_RUN);
    }


    @GetMapping("/test")
    public R test(){
        return R.ok().message("外网穿透测试成功。。。。");
    }

}
